﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UboxUI;
using User.Properties;

namespace User
{
    public partial class FrmUnboxing_article : FrmFather
    {
        public FrmUnboxing_article()
        {
            InitializeComponent();
            this.LblTitle.Text = this.Text;
            this.LblTitle2.Text = LblTitle.Text;
            this.BtnClose.Left = this.Width - this.BtnClose.Width - 10;
            this.BtnNarrow.Left = this.BtnClose.Left - this.BtnNarrow.Width - 10;
            this.panel1.Top = 0;
            this.panel2.Top = this.panel1.Height;
        }

        #region 初始宣告
        string conn = Settings.Default.strConn;
        string Identify = "main"; // 辨認用字別
        #endregion

        private void FrmUnboxing_article_Load(object sender, EventArgs e)
        {
            // TODO:  這行程式碼會將資料載入 'uBOXDataSet.brand' 資料表。您可以視需要進行移動或移除。
            this.brandTableAdapter.Fill(this.uBOXDataSet.brand);
            // TODO:  這行程式碼會將資料載入 'uBOXDataSet.category' 資料表。您可以視需要進行移動或移除。
            this.categoryTableAdapter.Fill(this.uBOXDataSet.category);

            DataGridViewFill();
           
        }


        private void Range(DataGridView dataGV)
        {
            dataGV.Columns["編號"].Width = 70;
            dataGV.Columns["編號"].Name = "編號";
            dataGV.Columns["類別"].Width = 100;
            dataGV.Columns["品牌"].Width = 100;
            dataGV.Columns["商品"].Width = 150;
            //dataGV.Columns["用戶帳號 "].Width = 150;
            dataGV.Columns["標題"].Width = 250;
            dataGV.Columns["內容"].Width = 200;
            dataGV.Columns["發表日期"].Width = 250;
            this.DataGridView.Columns["發表日期"].DefaultCellStyle.Format = "yyy/MM/dd  tt hh:mm:ss";
            dataGV.Columns["可見度"].Width = 100;
            
           
        }

        #region 顯示被檢舉的開箱文
        bool btnReportClick = false;
        private void btnReport_Click(object sender, EventArgs e)
        {
            btnReportClick = true;
            DataGridViewFill();
            btnReportClick = false;
        }
        #endregion

        private void btnSelect_Click(object sender, EventArgs e)
        {
            DataGridViewFill();
        }

        private void DataGridViewFill()
        {


            UnboxingLinqtoSqlDataContext db = new UnboxingLinqtoSqlDataContext(conn);

            var result = from u in db.unboxing_article
                                  join r in db.user_report on u.unboxing_article_id equals r.report_unboxing_id into temp
                                  from m in temp.DefaultIfEmpty()
                                 where
                                  (btnReportClick ? m.report_unboxing_id==u.unboxing_article_id : true) &&
                                  (chkUser.Checked ? u.user.user_username == this.txtUser.Text : true) &&
                                  (chkBrand.Checked ? u.production_product.brand.brand_id == (int)this.cboBrand.SelectedValue : true) &&
                                  (chkCategory.Checked ? u.production_product.category.category_id == (int)this.cboCategory.SelectedValue : true) &&
                                  (chkBrand.Checked ? u.production_product.brand.brand_id == (int)this.cboBrand.SelectedValue : true)
                                select new
                                {
                                    可見度 = u.unboxing_visible.ToString(),
                                    編號 = u.unboxing_article_id,
                                    類別 = u.production_product.category.category_name,
                                    品牌 = u.production_product.brand.brand_name,
                                    商品 = u.production_product.product_name,
                                    用戶帳號 = u.user.user_username,
                                    標題 = u.unboxing_article_title,
                                    內容 = u.unboxing_article_content,
                                    發表日期 = u.unboxing_article_publish_date,
                                    
                                };



            this.DataGridView.DataSource = result.ToList();

            Range(this.DataGridView);

            db.Dispose();
        }

        private void LoadByDate()
        {

        }

        private void btnhide_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("改變文章顯示狀態", "系統提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                int rowCount = this.DataGridView.SelectedRows.Count;
                using (UnboxingLinqtoSqlDataContext db = new UnboxingLinqtoSqlDataContext(conn))
                {
                    for (int i = 0; i < rowCount; i++)
                    {
                        int ID = (int)this.DataGridView.SelectedRows[i].Cells["編號"].Value;
                        var q = from u in db.unboxing_article
                                where u.unboxing_article_id == ID
                                select u;

                        foreach (var hide in q)
                        {
                            if (hide.unboxing_visible == true)
                            {
                                hide.unboxing_visible = false;
                            }
                            else
                            {
                                hide.unboxing_visible = true;
                            }
                        }
                    }
                    db.SubmitChanges();
                    DataGridViewFill();
                }

            }
            else
            {
                //...
            }
        }



        #region 按鈕樣式
        private void btnSelect_MouseEnter(object sender, EventArgs e)
        {
            this.btnSelect.FlatAppearance.BorderSize = 0;
            this.btnSelect.ForeColor = Color.White;
        }

        private void btnSelect_MouseLeave(object sender, EventArgs e)
        {
            this.btnSelect.FlatAppearance.BorderSize = 1;
            this.btnSelect.ForeColor = Color.Black;
        }

        private void btnhide_MouseEnter(object sender, EventArgs e)
        {
            this.btnhide.FlatAppearance.BorderSize = 0;
            this.btnhide.ForeColor = Color.White;
        }

        private void btnhide_MouseLeave(object sender, EventArgs e)
        {
            this.btnhide.FlatAppearance.BorderSize = 1;
            this.btnhide.ForeColor = Color.Black;
        }

        private void btnReport_MouseEnter(object sender, EventArgs e)
        {
            this.btnReport.FlatAppearance.BorderSize = 0;
            this.btnReport.ForeColor = Color.White;
        }

        private void btnReport_MouseLeave(object sender, EventArgs e)
        {
            this.btnReport.FlatAppearance.BorderSize = 1;
            this.btnReport.ForeColor = Color.Black;
        }
        #endregion

    }
}
